#include "datareceivedhandler.h"
#include "restaurantdatamanager.h"
#include "place.h"

#include <QVariant>
#include <QList>
#include <QJson/Parser>

/*
 *  parseJsonData used to parsing the result form JSON format to QVariant
 *  input  :  Data received from google API
 *  output :  the QVariant after parsed
*/
QVariant DataReceivedHandler::
parseJsonData(const QVariant & data)
{
    QJson::Parser parser;
    bool ok;

    QString strData( data.toString() );
    QByteArray latin1( strData.toLatin1() );

    QVariant result = parser.parse(latin1, & ok);

    if( ! ok ) {
        m_manager->errorOccured( QString("Cannot convert to QJson object:\n%1").arg(strData.left(32)).append("\n...") );
        return QVariant();
    }

    return result;
}


void LocationHandler::processData(const QVariant &data)
{
    QVariant parsedJson = parseJsonData(data);
    QString  status     = parsedJson.toMap()["status"].toString();
    QVariant results    = parsedJson.toMap()["results"];

    if (status == "OK" && results.isValid())
    {
        // Get the location information base on the first location found
        QVariantMap  location(results.toList()[0].toMap()["geometry"].toMap()["location"].toMap());
        QString      lat  = location["lat"].toString();
        QString      lng  = location["lng"].toString();
        QString      s_location = lat + "," + lng;
        m_manager->locationFound(s_location);

    }
    else
    {
        m_manager->errorOccured("The status of response is NOK!!!");
    }
}

void PlacesDataHandler::processData(const QVariant & data)
{
    QVariant parsedJson = parseJsonData(data);
    QString  status     = parsedJson.toMap()["status"].toString();
    QVariant results    = parsedJson.toMap()["results"];

    if (status == "OK" && results.isValid())
    {
       //Create a list to store place information
       //QList<Place*> places_list;

       // parsing the received place list, just get the interesting information
       QList<QVariant> received_place_list = results.toList();
       foreach(QVariant received_place, received_place_list)
       {
           QString place_id             = received_place.toMap()["place_id"].toString() ;
           m_manager->placeFound(place_id);
       }
    }
    else
    {
       m_manager->errorOccured("The status of response is NOK!!!");
    }
}

void PlaceDetailsHandler::processData(const QVariant & data)
{
    QVariant parsedJson = parseJsonData(data);
    QString  status     = parsedJson.toMap()["status"].toString();
    QVariant results    = parsedJson.toMap()["result"];
    if (status == "OK" && results.isValid())
    {
        QMap<QString, QVariant> place_details          = results.toMap();
        QString  place_id           = place_details["place_id"].toString();
        QString  name               = place_details["name"].toString();
        QString  address            = place_details["formatted_address"].toString();
        QString  phone_number       = place_details["formatted_phone_number"].toString();
        QString  place_reference    = place_details["reference"].toString();

        Place parsed_place_details(place_id, place_reference, name, address, phone_number);
        m_manager->placeDetailsFound(parsed_place_details);
    }
    else
    {
        m_manager->errorOccured("The status of response is NOK!!!");
    }
}
